package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.meily.web.entity.*;
import cn.openea.perpartner.meily.web.service.IShopProductService;
import cn.openea.perpartner.meily.web.service.IShopProductSkuService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-02-22 21:57
 */
@Api(tags = "SKU管理")
@RestController
@RequestMapping("sku")
public class ShopProductController extends BaseController {
    @Autowired
    private IShopProductService shopProductService;
    @Autowired
    private IShopProductSkuService productSkuService;


    @ApiOperation("SKU列表")
    @PostMapping("/skuData")
    public AjaxResult skuData(@RequestBody ShopProductSku sku) {

        return success(productSkuService.skuData(sku.getProductId()));
    }
    @ApiOperation("创建SKU")
    @PostMapping("/skuCreate")
    public AjaxResult skuCreate(@RequestBody JSONObject sku) {
        JSONObject productSku = productSkuService.skuCreate(sku);
        return BeanUtil.isNotEmpty(productSku) ? AjaxResult.success("SKU创建成功！", productSku) : error();
    }
    @ApiOperation("商品类型")
    @GetMapping("/productTypeData")
    public AjaxResult productTypeData() {
        List<ShopBaseProductType> list = shopProductService.productTypeData();
        return success(list);
    }

    @ApiOperation("创建商品类型")
    @PostMapping("/base/productTypeCreate")
    public AjaxResult productTypeCreate(@RequestBody ShopBaseProductType productType) {
        ShopBaseProductType baseProductType = shopProductService.productTypeCreate(productType);
        return BeanUtil.isNotEmpty(baseProductType) ? AjaxResult.success("商品类型创建成功！", baseProductType) : error();
    }

    @ApiOperation("删除商品类型")
    @PostMapping("/base/productTypeDelete")
    public AjaxResult productTypeDelete(@RequestBody ShopProductSpec spec) {

        return toAjax(shopProductService.productTypeDelete(spec.getId()));
    }

    @ApiOperation("创建商品类型属性值")
    @PostMapping("/base/productTypeValueCreate")
    public AjaxResult productTypeValueCreate(@RequestBody ShopBaseProductTypeAttribute productTypeAttribute) {
        ShopBaseProductTypeAttribute baseProductTypeAttribute = shopProductService.productTypeValueCreate(productTypeAttribute);
        return BeanUtil.isNotEmpty(baseProductTypeAttribute) ? AjaxResult.success("商品类型属性值创建成功！", baseProductTypeAttribute) : error();

    }

    @ApiOperation("删除商品类型值")
    @PostMapping("/base/productTypeValueDelete")
    public AjaxResult productTypeValueDelete(@RequestBody ShopProductSpec spec) {

        return toAjax(shopProductService.productTypeValueDelete(spec.getId()));
    }

    @ApiOperation("商品类型规格、属性")
    @GetMapping("/attrSpecData")
    public AjaxResult attrSpecData(String productTypeId, String productId) {

        return success(shopProductService.attrSpecData(productTypeId, productId));
    }

    @ApiOperation("创建商品类型属性")
    @PostMapping("/productAttrCreate")
    public AjaxResult productAttrCreate(@RequestBody List<ShopProductAttribute> productAttributeList) {

        return toAjax(shopProductService.productAttrCreate(productAttributeList));
    }

    @ApiOperation("创建规格")
    @PostMapping("/specCreate")
    public AjaxResult specCreate(@RequestBody ShopProductSpec spec) {
        ShopProductSpec productSpec = shopProductService.specCreate(spec);
        return BeanUtil.isNotEmpty(productSpec) ? AjaxResult.success("规格创建成功！", productSpec) : error();
    }

    @ApiOperation("删除规格")
    @PostMapping("/specDelete")
    public AjaxResult specDelete(@RequestBody ShopProductSpec spec) {

        return toAjax(shopProductService.specDelete(spec.getId()));
    }

    @ApiOperation("创建规格值")
    @PostMapping("/specValueCreate")
    public AjaxResult specValueCreate(@RequestBody ShopProductSpecValue specValue) {
        ShopProductSpecValue productSpecValue = shopProductService.specValueCreate(specValue);
        return BeanUtil.isNotEmpty(productSpecValue) ? AjaxResult.success("规格值创建成功！", productSpecValue) : error();
    }

    @ApiOperation("删除规格值")
    @PostMapping("/specValueDelete")
    public AjaxResult specValueDelete(@RequestBody ShopProductSpecValue specValue) {
        return toAjax(shopProductService.specValueDelete(specValue.getId()));
    }


    @ApiOperation("创建系统规格")
    @PostMapping("/base/specCreate")
    public AjaxResult baseSpecCreate(@RequestBody ShopBaseSpec spec) {
        ShopBaseSpec baseSpec = shopProductService.baseSpecCreate(spec);
        return BeanUtil.isNotEmpty(baseSpec) ? AjaxResult.success("系统规格创建成功！", baseSpec) : error();

    }

    @ApiOperation("删除系统规格")
    @PostMapping("/base/specDelete")
    public AjaxResult baseSpecDelete(@RequestBody ShopBaseSpec spec) {

        return toAjax(shopProductService.baseSpecDelete(spec.getId()));
    }

    @ApiOperation("创建系统规格值")
    @PostMapping("/base/specValueCreate")
    public AjaxResult baseSpecValueCreate(@RequestBody ShopBaseSpecValue specValue) {
        ShopBaseSpecValue baseSpecValue = shopProductService.baseSpecValueCreate(specValue);
        return BeanUtil.isNotEmpty(baseSpecValue) ? AjaxResult.success("系统规格值创建成功！", baseSpecValue) : error();
    }

    @ApiOperation("删除系统规格值")
    @PostMapping("/base/specValueDelete")
    public AjaxResult baseSpecValueDelete(@RequestBody ShopBaseSpecValue specValue) {
        return toAjax(shopProductService.baseSpecValueDelete(specValue.getId()));
    }

}
